package ca.etsmtl.log730.lab3.controllers2;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import ca.etsmtl.log730.lab3.events.bank.HelloEvent;
import ca.etsmtl.log730.lab3.model.Branch;

/*
 * JE SUIS LA CLASSE QUI GÈRE LES CONNEXIONS AU SERVEUR
 */
public class MainServerThread extends Thread {
	private int port;
	private ServerSocket serverSocket;
	
	/*
	 * 
	 */
	public void run() {
		try { 
			serverSocket = new ServerSocket(port); 
        } 
		catch (IOException e) 
        { 
			System.err.println("On ne peut pas écouter au  port: " + port + "."); 
			System.exit(1); 
        } 
		while(true) {
			Socket clientSocket = null;  
			try { 
				clientSocket = serverSocket.accept(); 
				System.out.println("Nouvelle connexion");
				Communicator communicator = new Communicator(clientSocket);
//				communicator.start();
				communicator.createInputHandler().start();
//				communicator.createOutputHandler().start();
				
			} catch(Exception e) {
	        	e.printStackTrace();
	        }
		}
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public ServerSocket getServerSocket() {
		return serverSocket;
	}

	public void setServerSocket(ServerSocket serverSocket) {
		this.serverSocket = serverSocket;
	}
}
